package org.dizena.modules.master.service;

import org.apache.commons.codec.digest.DigestUtils;
import org.dizena.base.shiro.SessionUserUtil;
import org.dizena.modules.master.avo.MenuVo;
import org.dizena.modules.master.bean.Res;
import org.dizena.modules.master.bean.Role;
import org.dizena.modules.master.bean.User;
import org.dizena.modules.master.dao.RessDao;
import org.dizena.modules.master.dao.UserDao;
import org.dizena.utils.ObjectUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WebRootService
{

    @Resource
    private UserDao userDao;
    @Resource
    private RessDao resDao;
    @Resource
    private RoleService roleService;


    public List<MenuVo> getUserRess()
    {
        List<MenuVo> menus = new ArrayList<MenuVo>();
        // 1,获得用户的角色
        User loginUser = SessionUserUtil.getSessionUser();
        List<String> roles = ObjectUtil.str2List(loginUser.getRoles(), ",");
        // 2,根据角色得到角色的资源
        Set<Res> allRess = new HashSet<Res>();
        for (String role : roles)
        {
            allRess.addAll(roleService.queryRoleRess(role));
        }

        // 3,分类按照级别排序
        List<Res> nowRess = new ArrayList<Res>(allRess);
        Collections.sort(nowRess);

        // 5,构建菜单数据结构
        menus = getMenus(nowRess, 1, "0");
        return menus;
    }

    private List<MenuVo> getMenus(List<Res> nowRess, Integer level, String pid)
    {
        List<MenuVo> menus = new ArrayList<MenuVo>();
        for (Res res : nowRess)
        {
            if (null != res && (level.equals(res.getResLevel())) && (pid.equals(res.getResPid())))
            {
                MenuVo m = new MenuVo(res.getResName(), res.getResUrl(), res.getResSort(), res.getResIcon());
                List<MenuVo> subs = getMenus(nowRess, res.getResLevel() + 1, res.getId());
                if (subs != null && subs.size() > 0)
                {
                    m.setSubs(subs);
                }
                menus.add(m);
            }
        }
        return menus;
    }

    public void initData()
    {
        Long cnt = userDao.count(null, new HashMap<>());
        if (cnt == null || cnt == 0)
        {
            String salt = ObjectUtil.makeNumberStr(8);
            String passwd = DigestUtils.md5Hex(salt + "12345678");
            Long ts = System.currentTimeMillis();

            User admin = new User("1", "admin@dizena.com", passwd, salt, "管理员", "master", "*", "token", 0, ts, ts);
            admin.setPic("http://cdn.huiye.tech/zoon/avatar/w11.jpg");
            userDao.add(admin);
            User admin2 = new User("2", "15001236918", passwd, salt, "管理员", "master", "*", "token", 0, ts, ts);
            admin2.setPic("http://cdn.huiye.tech/zoon/avatar/w11.jpg");
            userDao.add(admin2);

            Res r1 = new Res("1", "仪表盘", "/master/info", 1, 1, "0", "fa fa-home");
            Res r2 = new Res("2", "系统配置", "#", 2, 1, "0", "fa fa-gear");
            Res r21 = new Res("21", "用户管理", "/master/user/list", 201, 2, "2", "fa fa-star");
            Res r22 = new Res("22", "角色配置", "/master/role/list", 202, 2, "2", "fa fa-star");
            Res r23 = new Res("23", "菜单配置", "/master/ress/list", 203, 2, "2", "fa fa-star");
            Res r24 = new Res("24", "在线用户", "/master/line/list", 204, 2, "2", "fa fa-star");

            resDao.add(r1);
            resDao.add(r2);
            resDao.add(r21);
            resDao.add(r22);
            resDao.add(r23);
            resDao.add(r24);

            List<String> list = new ArrayList<>();
            list.add("1");
            list.add("2");
            list.add("21");
            list.add("22");
            list.add("23");
            list.add("24");

            Role role = new Role("1", "master", list);
            roleService.crud("add", role);
        }
    }

}
